package com.fita.api.dto.request;

import java.math.BigInteger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.opentravel.ota._2003._05.CompanyNameType;
import org.opentravel.ota._2003._05.OTAVehLocDetailRQ;
import org.opentravel.ota._2003._05.POSType;
import org.opentravel.ota._2003._05.SourceType;
import org.opentravel.ota._2003._05.SourceType.RequestorID;
import org.opentravel.ota._2003._05.TPAExtensionsType;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fita.api.dto.common.CommonVariable;

/**
 * 
 *
 * @author LZJ
 *
 */
public class LocationDetailRequest {
    
     /**
     * 车辆可用状态
     * 
     *  - Available=可用车辆
     *  - All=所有车辆
     */    
    private String status;
    
    private String brand;
    
    private String setCode;
    
    private String locationCode;
    
     /** 取车时间 */
    @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
    private String pickupDateTime;
    
    /** 还车时间 */
    @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
    private String returnDateTime;
    
    
    private String opScheduleIndicator;
    
    
    private String vendorCode;
    
    private String env;
    
    

    /**
     * The default constructor
     */
    public LocationDetailRequest() {
        
    }
    
    /**
     * @param env
     * @return
     */
    public LocationDetailRequest initialize(String env) {
        this.env = env;
        return this;
    }
    
    
    
    /**
     * @return
     * 
     * 校验获取的参数
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(status) &&
                StringUtils.isNotBlank(brand) &&
                StringUtils.isNotBlank(setCode) &&
                        locationCode != null && 
                        pickupDateTime != null &&
                        returnDateTime !=null &&
                        opScheduleIndicator !=null&&
                        vendorCode != null;
    }
    
    
    
    /**
     * @return
     * @throws ParserConfigurationException 
     *  
     */
    public OTAVehLocDetailRQ geneLocDeRq() throws ParserConfigurationException {
        
        OTAVehLocDetailRQ rq = new OTAVehLocDetailRQ();
        
        rq.setPrimaryLangID(CommonVariable.LANGUAGE);
        rq.setTarget(env);
        rq.setVersion(CommonVariable.VERSION);
        rq.setTransactionIdentifier("100000001");
        rq.setSequenceNmbr(new BigInteger("1"));
        
        POSType posType = this.genePos();
        rq.setPOS(posType);
        

        OTAVehLocDetailRQ.Location location = this.genelocationCode();
        rq.setLocation(location);
        
        CompanyNameType vendor = this.genevendor();
        rq.setVendor(vendor);
        
        OTAVehLocDetailRQ.Date date = this.genedate();
        rq.setDate(date);
        
        TPAExtensionsType extension = this.geneextension();
        
        rq.setTPAExtensions(extension);
        
        return rq;
    }
    
    /**
     * @return
     */
    private POSType genePos() {
        
        POSType pos = new POSType();
        
        // source1
        SourceType sourceType1 = new SourceType();
        sourceType1.setISOCountry("CN");
        
        // requestID
        RequestorID requestorID = new RequestorID();
        requestorID.setType(CommonVariable.REQUESTOR_TYPE);
        requestorID.setID(setCode);
        
        // companyName
        CompanyNameType companyName = new CompanyNameType();
        if(StringUtils.equalsAnyIgnoreCase(env, "Production")) {
            companyName.setCode("CU");
            companyName.setCompanyShortName("CNTVLSVCXML");
        } else if(StringUtils.equalsAnyIgnoreCase(env, "Testing")) {
            companyName.setCode("EX");
            companyName.setCompanyShortName("EHIXMLTEST");
        }
        
        requestorID.setCompanyName(companyName);
        sourceType1.setRequestorID(requestorID);
        
        // source2
        SourceType sourceType2 = new SourceType();
        
        // requestID
        requestorID = new RequestorID();
        requestorID.setType("4");
        requestorID.setID(this.brand);
        requestorID.setIDContext("IATA");
        
        sourceType2.setRequestorID(requestorID);
        
        pos.getSource().add(sourceType1);
        pos.getSource().add(sourceType2);
        
        return pos;
        
    }
    
    //location
    private OTAVehLocDetailRQ.Location genelocationCode () {
        
        OTAVehLocDetailRQ.Location location = new OTAVehLocDetailRQ.Location();

        location.setLocationCode(this.locationCode);
        
        return location;
    }
    
    //vendor
    private CompanyNameType genevendor() {
        
        CompanyNameType vendor = new CompanyNameType();
        
        vendor.setCode(this.vendorCode);
        
        return  vendor;
    }
    
    //Date
    
    private OTAVehLocDetailRQ.Date genedate () {
        
        
        OTAVehLocDetailRQ.Date date = new OTAVehLocDetailRQ.Date();
        
        date.setPickUpDateTime(this.pickupDateTime);
        date.setReturnDateTime(this.returnDateTime);
        
        
        return date;
    }
    
    
    //tpaExtensions
    
    private TPAExtensionsType geneextension () throws ParserConfigurationException {
        
        TPAExtensionsType extension = new TPAExtensionsType();
        
        DocumentBuilderFactory dbFactory =
        DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.newDocument();
        
        Element element = doc.createElement("TPA_Extension_Flag");
        Attr attr = doc.createAttribute("OpScheduleIndicator");
        attr.setValue(this.opScheduleIndicator);
        element.setAttributeNode(attr);
        
        extension.getAny().add(element);
        
        return extension;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @return the brand
     */
    public String getBrand() {
        return brand;
    }

    /**
     * @return the setCode
     */
    public String getSetCode() {
        return setCode;
    }

    /**
     * @return the locationCode
     */
    public String getLocationCode() {
        return locationCode;
    }

    /**
     * @return the pickupDateTime
     */
    public String getPickupDateTime() {
        return pickupDateTime;
    }

    /**
     * @return the returnDateTime
     */
    public String getReturnDateTime() {
        return returnDateTime;
    }

    /**
     * @return the opScheduleIndicator
     */
    public String getOpScheduleIndicator() {
        return opScheduleIndicator;
    }

    /**
     * @return the vendorCode
     */
    public String getVendorCode() {
        return vendorCode;
    }

    /**
     * @return the env
     */
    public String getEnv() {
        return env;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @param brand the brand to set
     */
    public void setBrand(String brand) {
        this.brand = brand;
    }

    /**
     * @param setCode the setCode to set
     */
    public void setSetCode(String setCode) {
        this.setCode = setCode;
    }

    /**
     * @param locationCode the locationCode to set
     */
    public void setLocationCode(String locationCode) {
        this.locationCode = locationCode;
    }

    /**
     * @param pickupDateTime the pickupDateTime to set
     */
    public void setPickupDateTime(String pickupDateTime) {
        this.pickupDateTime = pickupDateTime;
    }

    /**
     * @param returnDateTime the returnDateTime to set
     */
    public void setReturnDateTime(String returnDateTime) {
        this.returnDateTime = returnDateTime;
    }

    /**
     * @param opScheduleIndicator the opScheduleIndicator to set
     */
    public void setOpScheduleIndicator(String opScheduleIndicator) {
        this.opScheduleIndicator = opScheduleIndicator;
    }

    /**
     * @param vendorCode the vendorCode to set
     */
    public void setVendorCode(String vendorCode) {
        this.vendorCode = vendorCode;
    }

    /**
     * @param env the env to set
     */
    public void setEnv(String env) {
        this.env = env;
    }
}
